<template>
  <div class="login-container">
    <div class="login-card">
      <h2>用户登录</h2>
      <div class="admin-tip">
        <p>管理员账户：</p>
        <p>用户名：admin</p>
        <p>密码：admin123</p>
      </div>
      
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名：</label>
          <input
            type="text"
            id="username"
            v-model="username"
            required
            placeholder="请输入用户名"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码：</label>
          <input
            type="password"
            id="password"
            v-model="password"
            required
            placeholder="请输入密码"
          />
        </div>
        
        <div class="form-actions">
          <button type="submit" class="btn btn-primary" :disabled="isLoading">
            {{ isLoading ? '登录中...' : '登录' }}
          </button>
          <router-link to="/register" class="btn btn-link">没有账号？立即注册</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import api from '../api/config'

const router = useRouter()
const username = ref('')
const password = ref('')
const isLoading = ref(false)

const handleLogin = async () => {
  isLoading.value = true
  
  try {
    console.log('开始登录请求...')
    const response = await api.post('http://172.31.53.239:5000/api/login', {
      username: username.value,
      password: password.value
    })
    
    console.log('登录响应:', response.data)
    
    if (response.data.token && response.data.user) {
      // 保存token和用户信息
      localStorage.setItem('token', response.data.token)
      localStorage.setItem('username', response.data.user.username)
      localStorage.setItem('userRole', response.data.user.role)
      
      console.log('用户信息已保存到localStorage')
      
      // 触发 storage 事件以更新导航栏
      window.dispatchEvent(new Event('storage'))
      
      // 根据角色跳转到不同页面
      if (response.data.user.role === 'admin') {
        console.log('管理员登录成功，跳转到设置页面')
        router.push('/settings')
      } else {
        console.log('普通用户登录成功，跳转到识别页面')
        router.push('/prediction')
      }
    } else {
      console.error('登录响应数据不完整:', response.data)
      alert('登录响应数据不完整，请重试')
    }
  } catch (error) {
    console.error('登录失败:', error)
    if (error.response) {
      console.error('错误响应:', error.response.data)
      alert(error.response.data.message || '登录失败，请检查用户名和密码')
    } else if (error.request) {
      console.error('请求错误:', error.request)
      alert('无法连接到服务器，请检查网络连接')
    } else {
      console.error('其他错误:', error.message)
      alert('登录过程中发生错误: ' + error.message)
    }
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background-color: #f5f5f5;
}

.login-card {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.admin-tip {
  background-color: #e8f5e9;
  padding: 1rem;
  border-radius: 4px;
  margin-bottom: 1.5rem;
}

.admin-tip p {
  margin: 0.5rem 0;
  color: #2e7d32;
}

.admin-tip p:first-child {
  font-weight: bold;
  margin-bottom: 0.5rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-group label {
  color: #666;
}

.form-group input {
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

.form-group input:focus {
  outline: none;
  border-color: #42b983;
}

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  align-items: center;
}

.btn {
  width: 100%;
  padding: 0.75rem;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-primary {
  background-color: #42b983;
  color: white;
}

.btn-primary:hover {
  background-color: #3aa876;
}

.btn-primary:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.btn-link {
  background: none;
  color: #42b983;
  text-decoration: none;
}

.btn-link:hover {
  text-decoration: underline;
}
</style> 